package cn.cdutetc.foxwatch.serveice.imp;

import cn.cdutetc.foxwatch.mapper.DemoMapper;
import cn.cdutetc.foxwatch.pojo.DemoPojo;
import cn.cdutetc.foxwatch.pojo.PagePojo;
import cn.cdutetc.foxwatch.query.DemoQuery;
import cn.cdutetc.foxwatch.serveice.DemoService;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class DemoServiceImpl extends ServiceImpl<DemoMapper, DemoPojo> implements DemoService {
    @Override
    public PagePojo<DemoPojo> queryDemosPage(DemoQuery demoQuery) {
        String name = demoQuery.getName();
        Boolean isAsc = demoQuery.getIsAsc();
        String orderBy = demoQuery.getSortBy();
        if (isAsc == null) isAsc = false;
        if (orderBy == null) orderBy = "create_time";
        Page<DemoPojo> page = Page.of(demoQuery.getPageNum(), demoQuery.getPageSize());
        page.addOrder(new OrderItem().setAsc(isAsc).setColumn(orderBy));
        Page<DemoPojo> pages = lambdaQuery().like(name != null, DemoPojo::getName, name)
                .page(page);
        PagePojo<DemoPojo> pojo = new PagePojo<>();
        pojo.setTotal(pages.getTotal());
        pojo.setList(pages.getRecords());
        pojo.setPages(pages.getPages());
        return pojo;
    }
}
